package com.atguigu.rabbitmq.helloworld.Producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * rabbitmq-producer
 */

@SuppressWarnings("all")
public class Producer {

    private final String QUEUE_NAME = "HELLO"; // 设置rabbitmq队列的名称

    private final String HOST = "127.0.0.1";

    private final String USERNAME = "admin";

    private final String PASSWORD = "root";


    public ConnectionFactory getFactory() {
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost(HOST);

        factory.setUsername(USERNAME);

        factory.setPassword(PASSWORD);

        return factory;
    }

    public Channel getChannel() {
        ConnectionFactory factory = getFactory();

        Connection connection = null;
        Channel channel = null;
        try {
            connection = factory.newConnection();

            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return channel;
    }


    /**
     * 生成一个rabbitmq队列
     *   queueDeclare参数声明：
     *      1. 设置rabbitmq队列名称
     *
     *      2. 是否持久化发送的数据
     *
     *      3. 是否只提供给一个消费者进行消费
     *
     *      4. 是否自动删除
     *
     *      5. 发送数据的其他参数，可以传递一个map
     *
     * 发送消息到队列中
     *    basicPublish参数声明：
     *      1.  设置使用那个交换机进行消息发送
     *
     *      2.  设置发送消息到那个队列
     *
     *      3.  其他参数
     *
     *      4.  发送的数据，格式是byte数组
     * @param content
     */
    public void SeedMessage(String content) {
        Channel channel = getChannel();

        try {

            // 生成一个rabbitmq队列
            channel.queueDeclare(QUEUE_NAME, false, false, true, null);

            // 发送消息到队列中
            channel.basicPublish("", QUEUE_NAME, null, content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        Producer producer = new Producer();
        producer.SeedMessage("hello wolrd");
    }
}




























